Ext.ns('Wlanauth.user');
/**
 * 导出配置窗
 */
Wlanauth.user.ExcelConfigWindow = Ext.extend(Ext.Window,{
	title:'导出选项',
	iconCls:'edit',
	width:460,
	closeAction:'close',
	resizable:false,
	border:false,
	grid:null,
	constructor:function(config){
		Ext.apply(this,config);
		
		this.tipLabel = new Ext.Toolbar.TextItem();
		var p = {start:0,limit:20};
		var opts = this.grid.getStore().lastOptions;
		if(opts!=null&&opts.params){
			p = opts.params;
		}
		
		Wlanauth.user.ExcelConfigWindow.superclass.constructor.call(this,{
			
			items:[new Ext.form.FormPanel({
				padding:5,
				labelWidth:80,
				items:[{
					fieldLabel:'记录',
					xtype:'radiogroup',
					name:'exportTypeGroup',
					items:[{
						name:'config.exportType',boxLabel:'当前页',checked:true,inputValue:'PAGE'
					},{
						name:'config.exportType',boxLabel:'当前选中',inputValue:'SELECTED'
					},{
						name:'config.exportType',boxLabel:'全部数据',inputValue:'ALL'
					},{
						name:'config.exportType',boxLabel:'自定范围',inputValue:'RANGE'
					}],
					listeners:{
						'change': function(rg,checkedRadio){
							rg.nextSibling().setVisible(checkedRadio.inputValue=='RANGE');
							rg.nextSibling().setDisabled(!(checkedRadio.inputValue=='RANGE'));
							this.doLayout();
						},
						scope:this
					}
				},{
					fieldLabel:'导出范围',
					xtype: 'compositefield',
					hidden:true,
					items:[{
						xtype:'displayfield',value:'从'
					},{
						xtype:'numberfield',value:p.start,width:60,name:'config.start'
					},{
						xtype:'displayfield',value:'开始,取'
					},{
						xtype:'numberfield',name:'config.limit',width:60,value:p.limit
					},{
						xtype:'displayfield',value:'条记录'
					}]
				},{
					fieldLabel:'导出列',
					xtype:'checkboxgroup',
					columns: 4,
					disabled:false,
					items:[{
						boxLabel:'卡号',checked:true,inputValue:'username',name:'config.propertyNames[0]'
					},{
						boxLabel:'PUK',checked:true,inputValue:'puk',name:'config.propertyNames[1]'
					},{
						boxLabel:'绑定IP',checked:true,inputValue:'bindIP',name:'config.propertyNames[2]'
					},{
						boxLabel:'有效期限',checked:true,inputValue:'expiredTime',name:'config.propertyNames[3]'
					},{
						boxLabel:'额定流量',checked:true,inputValue:'maxOctets',name:'config.propertyNames[4]'
					},{
						boxLabel:'剩余流量',inputValue:'remainOctets',name:'config.propertyNames[5]'
					},{
						boxLabel:'已用流量',inputValue:'sumOctets',name:'config.propertyNames[6]'
					},{
						boxLabel:'额定金额',checked:true,inputValue:'actualFee',name:'config.propertyNames[7]'
					},{
						boxLabel:'剩余金额',inputValue:'remainFee',name:'config.propertyNames[8]'
					},{
						boxLabel:'最后登陆',inputValue:'lastAcctStartTime',name:'config.propertyNames[9]'
					},{
						boxLabel:'导入批次',inputValue:'batchId',name:'config.propertyNames[10]'
					}]
				},{
					fieldLabel:'其它',
					xtype:'checkbox',
					boxLabel:'统计总数',
					inputValue:'true',
					name:'config.showTotal'
				}]
			})],
			buttons:[this.tipLabel,{
				text:'确定',
				handler:this.doSubmit,
				scope:this
			},{
				text:'取消',
				scope:this,
				handler:function(){
					this.close();
				}
			}]
		});
	},
	doSubmit:function(){
		var _url = 'excel!exportUser.action';
		var status = this.grid.userStatusType;
		if(status=='HISTORY'){
			_url = 'excel!exportUserHis.action';
		}
		var fp = this.items.itemAt(0);
		var exportType = fp.getForm().findField('exportTypeGroup').getValue().inputValue;
		var rs = null;
		if(exportType=='PAGE'){
			rs = this.grid.getStore().getRange();
		} else if(exportType == 'SELECTED'){
			rs = this.grid.getSelectionModel().getSelections();
		}
		var ids = '';
		if(rs!=null){
			if(rs.length<1){
				Ext.Msg.alert('提示','无任何记录');
				return ;
			}
			for(var i=0;i<rs.length;i++){
				ids+=','+rs[i].get('id');
			}
			ids = (ids==''?'':ids.substring(1));
		}
		
		this.tipLabel.setText('正在生成Excel,请稍等...');
		fp.getForm().submit({
			url:_url,
			params:{userIds:ids},
			success:function(form,action){
				if(action.result.success){
					fp.ownerCt.close();
					window.location.href=action.result.data;
				} else {
					if(action.result.data){
						Ext.Msg.alert('提示',action.result.data);
					} else {
						Ext.Msg.alert('提示','生成EXCEL失败');
					}
				}
			},
			failure:function(form,action){
				Ext.Msg.alert('提示','生成EXCEL失败!');
			}
		});
	}
})